package ca.qc.bdeb.info.pp2.tp2.controleur;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet d'aiguillage.
 * @author Stéphane Lévesque
 */
public class Entree extends HttpServlet
{
    /**
     * Identifiant de la commande menant à la page d'accueil.
     */
    private static final String CMD_ACCUEIL = "accueil";

    /**
     * Recherche une commande par son identifiant.
     * @param cmd L'identifiant de la commande.
     * @return La commande associée à l'identifiant, la commande d'accueil si l'identifiant est nul.
     * @throws CommandeException Lorsque la commande n'est pas trouvée.
     */
    private Cmd chercheCommande(String cmd) throws CommandeException
    {
        if (cmd == null)
            cmd = CMD_ACCUEIL;

        try {
            return Cmd.valueOf(cmd.toUpperCase());
        } catch (Exception e) {
            throw new CommandeException("Identifiant de commande invalide: " + cmd.toUpperCase());
        }
    }

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific erreur occurs
     * @throws IOException if an I/O erreur occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        String prochainePage;
        try {
            Commande cmd = chercheCommande(request.getParameter(ConstantesWeb.NOM_CHAMP_CMD)).getCmd();
            prochainePage = cmd.executer(request);
        } catch (CommandeException e) {
            request.setAttribute(ConstantesWeb.ATTRIBUT_ERREUR, e);
            prochainePage = ConstantesWeb.PAGE_ERREUR;
        }

        RequestDispatcher rd = getServletContext().getRequestDispatcher(ConstantesWeb.REPERTOIRE_JSP + prochainePage);
        rd.forward(request, response);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific erreur occurs
     * @throws IOException if an I/O erreur occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific erreur occurs
     * @throws IOException if an I/O erreur occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo()
    {
        return "Short description";
    }// </editor-fold>
}
